package com.disdr.picture;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class 签收单填充 {


    public static void main(String[] args) throws Exception {

        String 石头人 = "4486197917,2022/10/27,豫EG8119,钢材,34.52t;4486097917,2022/10/26,豫EG8119,钢材,34.52t;4485997917,2022/10/25,豫EG8119,钢材,35t;4485497917,2022/10/20,豫EG8119,钢材,34t;4485297917,2022/10/18,豫EG8119,钢材,33t;4486197917,2022/10/27,冀T28161,钢材,32t;4485797917,2022/10/23,冀T28161,钢材,34t;4485497917,2022/10/20,冀T28161,钢材,32t;4484897917,2022/10/14,冀T28161,钢材,33t";
        for(String str : 石头人.split(";")){
            generateReceipt(str.split(","));
        }
    }
    /**
     * 环运回单
     *
     * @return
     * @throws IOException
     */
    public static BufferedImage generateReceipt(String[] obj) throws Exception {

        //BufferedImage bi = ImageIO.read(new File("F://gbd.jpg"));
        File file = new File("src\\main\\resources\\templates\\qsd3.jpg");
        //System.out.println(file.getAbsolutePath());
        BufferedImage bi = ImageIO.read(file);
        //System.out.println(bi.getWidth());//841
        //System.out.println(bi.getHeight());//332

        // 得到它的绘制环境(这张图片的笔)
        Graphics2D g2 = (Graphics2D) bi.getGraphics();
        Font font = new Font("腾祥伯当草书", 0, 25);
        g2.setFont(font);
        g2.setColor(Color.GRAY);// 设置字体颜色

        //DateUtil.parse(obj[0])
        g2.drawString(obj[1], 210, 92);
        g2.drawString(obj[2], 210, 127);
        g2.drawString("耀然贸易", 210, 162);
        //g2.drawString("19500", 220, 197);
        //g2.drawString(".getAdvance()", 210, 232);
        //g2.drawString(".getAdvance()", 210, 267);

        //g2.drawString(obj[1].substring(11), 550, 92);
        g2.drawString(obj[3], 550, 127);
        //g2.drawString("57380", 560, 162);
        //Integer ii = 28000 + (int)(Math.random()*8000);
        g2.drawString(obj[4], 560, 197);
        //g2.drawString("24", 550, 232);
        //g2.drawString("", 550, 267);

        g2.drawString("B001", 195, 310);
        g2.dispose();//释放对象

        BufferedImage source = insertImage(bi);
        ImageIO.write(source, "jpg", new FileOutputStream("D:\\user\\Desktop\\"+obj[0]+".jpg"));// 保存图片 JPEG表示保存格式
        return bi;

    }

    /**
     * 补充背景
     *
     * @param src 签收单照片
     * @throws Exception
     */
    private static BufferedImage insertImage(BufferedImage src) throws Exception {
        File f = new File("disdr");
        if (!f.exists()) {
            f.mkdir();
        }
        File file = new File("D:\\user\\Desktop\\桌子背景.jpg");

        BufferedImage source = ImageIO.read(file);
        int width = src.getWidth();
        int height = src.getHeight();
        int w = (int)(Math.random()*250)+188;
        int h = (int)(Math.random()*150)+88;

        BufferedImage 桌子背景 = source.getSubimage(0,0,width+w,height+h);

        //合并照片
        Graphics2D graph = 桌子背景.createGraphics();
        graph.drawImage(src, 188, 88, width, height, null);
        /*Shape shape = new RoundRectangle2D.Float(8, 8, width, width, 6, 6);
        graph.setStroke(new BasicStroke(3f));
        graph.draw(shape);*/
        graph.dispose();
        return 桌子背景;
    }
}
